package com.preop.lucene.utils;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import java.io.*;
import java.util.Date;
import java.util.StringTokenizer;

/** Index all text files under a directory. */
public class IndexFiles {

	private IndexFiles() {
	}

	static final File INDEX_DIR = new File("index");

	/** Index all text files under a directory. */
	public static void main(String[] args) {
		String usage = "java com.preop.lucene.utils.IndexFiles <doc-type> <root_directory>";
		if (args.length == 0) {
			System.err.println("Usage: " + usage);
			System.exit(1);
		}

		if (INDEX_DIR.exists()) {
			/*
			 * System.out.println("Cannot save index to '" + INDEX_DIR + "'
			 * directory, please delete it first"); System.exit(1);
			 */
		}

		final File docDir = new File(args[0]);
		INDEX_DIR.delete();
		if (!docDir.exists() || !docDir.canRead()) {

			/*
			 * System.out .println("Document directory '" +
			 * docDir.getAbsolutePath() + "' does not exist or is not readable,
			 * please check the path"); System.exit(1);
			 */
		}

		Date start = new Date();
		try {
			IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true);
			System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
			indexDocs(writer, docDir);
			System.out.println("Optimizing...");
			writer.optimize();
			writer.close();

			Date end = new Date();
			System.out.println(end.getTime() - start.getTime() + " total milliseconds");

		} catch (IOException e) {
			System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
		}
	}

	static void indexDocs(IndexWriter writer, File file) throws IOException {
		// do not try to index files that cannot be read
		if (file.canRead()) {
			if (file.isDirectory()) {
				String[] files = file.list();
				// an IO error could occur
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						System.out.println("Read " + files[i]);
						indexDocs(writer, new File(file, files[i]));
					}
				}
			} else {

				String line;
				BufferedReader br = new BufferedReader(new FileReader(file));
				Document doc = null;
				while ((line = br.readLine()) != null) {
					if (!line.startsWith("#")) {
						StringTokenizer matcher = new StringTokenizer(line);
						doc = new Document();
						doc.add(new Field("date", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("time", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("s-sitename", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("s-ip", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("cs-method", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("cs-uri-stem", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("cs-uri-query", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						doc.add(new Field("s-port", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						//doc.add(new Field("cs-username", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						//doc.add(new Field("c-ip", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						//doc.add(new Field("cs(User-Agent)", matcher.nextToken().toLowerCase(), Field.Store.YES, Field.Index.UN_TOKENIZED));
						writer.addDocument(doc);
					}
				}
			}
		}
	}

}
